﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;

namespace WebUI.EditPages
{
    public partial class AddService : BasePage
    {
        Contracts contract;
        int contractID;
        string mode;

        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);

            if (Request.QueryString[KEY] == null)
            {
                CloseDlg(0);
                return;
            }

            if (Request.QueryString[MODE] == null)
            {
                CloseDlg(0);
                return;
            }

            mode = Request.QueryString[MODE];

            if (Request.QueryString[ID] == null && (mode == EDIT_MODE || mode == VIEW_MODE))
            {
                CloseDlg(0);
                return;
            }

            contract = (Contracts)Session[Request.QueryString[KEY]];
            string oldFilter = contract.Filter;

            contractID = Request.QueryString[ID] != null ? int.Parse(Request.QueryString[ID]) : 0;
            if (mode != ADD_MODE)
            {
                contract.Filter = Contracts.ColumnNames.ID + "=" + contractID.ToString();
            }
            else
            {
                btnSave.Enabled = false;
            }

            if (!IsPostBack && contract.RowCount > 0 && mode != ADD_MODE)
                LoadInfo();

            contract.Filter = oldFilter;
        }

        public void btnSave_OnClick(Object sender, EventArgs e)
        {
            Save();
            ScriptManager.RegisterClientScriptBlock(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("window.returnValue={0};", 1), true);
        }

        private bool Save()
        {
            string oldFilter = contract.Filter;
            try
            {
                if (mode == ADD_MODE)
                {
                    contract.AddNew();
                    contract.Deleted = false;
                    contract.MPP = 0;
                    contract.Commission = 0;
                    contract.Rebate = 0;
                }
                else
                {
                    contract.Filter = Contracts.ColumnNames.ID + "=" + contractID;
                }

                contract.LastModifiedByUser = SessionUser.ID;
                contract.LastModifiedDate = DateTime.UtcNow.AddHours(SysVars.TimeZone);
                contract.PlanDescription = addService.PlanDescription;
                contract.PlanDuration = addService.PlanDuration;
                contract.Rebate = addService.Rebate;
                contract.MPP = addService.MPP;
                contract.Commission = addService.Commission;
                contract.NetworkID = addService.NetworkID;
                contract.MobileNumber = addService.MobileNumber;
                contract.Network = addService.Network;
                if(addService.StartDate != null)
                    contract.ContractStartDate = (DateTime)addService.StartDate;
                if (addService.EndDate != null)
                    contract.ContractEndDate = (DateTime)addService.EndDate;
                if (addService.CreatedDate != null)
                    contract.ContractCreatedDate = (DateTime)addService.CreatedDate;
  
                //contract.Save();
                contract.Filter = oldFilter;
                ((Popup)Master).ShowMessage("Saved");
                return true;
            }
            catch (Exception ex)
            {
                contract.Filter = oldFilter;

                ((Popup)Master).ShowErrorMessage(ex.Message);
                ErrorLog.WriteToFile("addService.aspx.cs", "Save", ex.StackTrace);
                return false;
            }
        }

        private void LoadInfo()
        {
            try
            {
                if (!contract.IsColumnNull(Contracts.ColumnNames.MPP))
                    addService.MPP = contract.MPP;
                if (!contract.IsColumnNull(Contracts.ColumnNames.Rebate))
                    addService.Rebate = contract.Rebate;
                if (!contract.IsColumnNull(Contracts.ColumnNames.NetworkID))
                    addService.NetworkID = contract.NetworkID;
                if (!contract.IsColumnNull(Contracts.ColumnNames.PlanDescription))
                    addService.PlanDescription = contract.PlanDescription;
                if (!contract.IsColumnNull(Contracts.ColumnNames.PlanDuration))
                    addService.PlanDuration = contract.PlanDuration;
                if (!contract.IsColumnNull(Contracts.ColumnNames.MobileNumber))
                    addService.MobileNumber = contract.MobileNumber;
                if (!contract.IsColumnNull(Contracts.ColumnNames.Commission))
                    addService.Commission = contract.Commission;
                if (!contract.IsColumnNull(Contracts.ColumnNames.ContractStartDate))
                    addService.StartDate = contract.ContractStartDate;
                else
                    addService.StartDate = DateTime.UtcNow.AddHours(SysVars.TimeZone);
                if (!contract.IsColumnNull(Contracts.ColumnNames.ContractEndDate))
                    addService.EndDate = contract.ContractEndDate;
                if (!contract.IsColumnNull(Contracts.ColumnNames.ContractCreatedDate))
                    addService.CreatedDate = contract.ContractCreatedDate;
                else
                    addService.CreatedDate = DateTime.UtcNow.AddHours(SysVars.TimeZone);
            }
            catch (Exception ex)
            {
                ((Popup)Master).ShowErrorMessage(ex.Message);
                ErrorLog.WriteToFile("addService.aspx.cs", "LoadInfo", ex.StackTrace);
            }
        }

        public void btnSaveAndClose_OnClick(Object sender, EventArgs e)
        {
            if (Save())
                CloseDlg(1);
        }
    }
}
